<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>嵌入式实验3-文件io编程 | hatena</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="一、实验目的1.掌握文件读写及上锁操作2.掌握open()、read()、write()、fcntl()等函数的使用">
<meta property="og:type" content="article">
<meta property="og:title" content="嵌入式实验3-文件io编程">
<meta property="og:url" content="https://gitee.com/lanceluot/blog.git/2024/05/01/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%AE%9E%E9%AA%8C3-%E6%96%87%E4%BB%B6io%E7%BC%96%E7%A8%8B/index.html">
<meta property="og:site_name" content="hatena">
<meta property="og:description" content="一、实验目的1.掌握文件读写及上锁操作2.掌握open()、read()、write()、fcntl()等函数的使用">
<meta property="og:locale" content="zn_CN">
<meta property="og:image" content="https://gitee.com/blog/img/wps3.jpg">
<meta property="og:image" content="https://gitee.com/blog/img/wps4.jpg">
<meta property="og:image" content="https://gitee.com/blog/img/wps5.jpg">
<meta property="og:image" content="https://gitee.com/blog/img/wps6.jpg">
<meta property="og:image" content="https://gitee.com/blog/img/wps7.jpg">
<meta property="og:image" content="https://gitee.com/blog/img/wps8.jpg">
<meta property="article:published_time" content="2024-05-01T05:44:36.000Z">
<meta property="article:modified_time" content="2024-05-18T15:48:46.256Z">
<meta property="article:author" content="myself">
<meta property="article:tag" content="实验">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/blog/img/wps3.jpg">
  
    <link rel="alternate" href="/blog/atom.xml" title="hatena" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/blog/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/blog/css/style.css">

  
    
<link rel="stylesheet" href="/blog/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/blog/" id="logo">hatena</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/blog/">Home</a>
        
          <a class="main-nav-link" href="/blog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/blog/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Suche"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Suche"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/lanceluot/blog.git"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-嵌入式实验3-文件io编程" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog/2024/05/01/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%AE%9E%E9%AA%8C3-%E6%96%87%E4%BB%B6io%E7%BC%96%E7%A8%8B/" class="article-date">
  <time class="dt-published" datetime="2024-05-01T05:44:36.000Z" itemprop="datePublished">2024-05-01</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      嵌入式实验3-文件io编程
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="一、实验目的"><a href="#一、实验目的" class="headerlink" title="一、实验目的"></a>一、实验目的</h2><h3 id="1-掌握文件读写及上锁操作"><a href="#1-掌握文件读写及上锁操作" class="headerlink" title="1.掌握文件读写及上锁操作"></a>1.掌握文件读写及上锁操作</h3><h3 id="2-掌握open-、read-、write-、fcntl-等函数的使用"><a href="#2-掌握open-、read-、write-、fcntl-等函数的使用" class="headerlink" title="2.掌握open()、read()、write()、fcntl()等函数的使用"></a>2.掌握open()、read()、write()、fcntl()等函数的使用</h3> <span id="more"></span>

<h2 id="二、-实验内容与设计思想"><a href="#二、-实验内容与设计思想" class="headerlink" title="二、 实验内容与设计思想"></a>二、 <strong>实验内容与设计思想</strong></h2><h3 id="1-编写程序实现文件写入锁和读取锁的设置和运行"><a href="#1-编写程序实现文件写入锁和读取锁的设置和运行" class="headerlink" title="1. 编写程序实现文件写入锁和读取锁的设置和运行"></a>1. 编写程序实现文件写入锁和读取锁的设置和运行</h3><h3 id="2-编写程序使用文件操作，仿真FIFO（先进先出）结构以及生产者-消费者运行模型"><a href="#2-编写程序使用文件操作，仿真FIFO（先进先出）结构以及生产者-消费者运行模型" class="headerlink" title="2. 编写程序使用文件操作，仿真FIFO（先进先出）结构以及生产者-消费者运行模型"></a>2. 编写程序使用文件操作，仿真FIFO（先进先出）结构以及生产者-消费者运行模型</h3><h3 id="3-编程程序实现文件多路复用操作。"><a href="#3-编程程序实现文件多路复用操作。" class="headerlink" title="3. 编程程序实现文件多路复用操作。"></a>3. 编程程序实现文件多路复用操作。</h3><h2 id="三、实验使用环境"><a href="#三、实验使用环境" class="headerlink" title="三、实验使用环境"></a><strong>三、实验使用环境</strong></h2><p>Ubuntu虚拟机</p>
<h2 id="四、实验步骤和调试过程"><a href="#四、实验步骤和调试过程" class="headerlink" title="四、实验步骤和调试过程"></a>四、实验步骤和调试过程</h2><h3 id="1-文件写入锁和读取锁"><a href="#1-文件写入锁和读取锁" class="headerlink" title="1.文件写入锁和读取锁"></a>1.文件写入锁和读取锁</h3><p>1）编程lock_set.c实现文件记录锁功能</p>
<p>2）编写文件写入锁的测试用例write_lock.c：创建一个hello文件，之后对其上写入锁，键盘输入任意一个字符后解除写入锁。</p>
<p>Write_lock.c:</p>
<p><img src="/blog/img/wps3.jpg" alt="img"> </p>
<p>运行可执行文件,结果如下：</p>
<p><img src="/blog/img/wps4.jpg" alt="img"> </p>
<p>3）在两个终端上运行.&#x2F;write_lock，查看运行结果</p>
<p>在两个终端上运行如图：</p>
<p><img src="/blog/img/wps5.jpg" alt="img"> </p>
<p>4）编写文件读取锁的测试用例read_lock.c：创建一个hello文件，之后对其上读取锁，键盘输入任意一个字符后解除读取锁。</p>
<p><img src="/blog/img/wps6.jpg" alt="img"> </p>
<p>5）在两个终端上运行.&#x2F;read_lock，查看运行结果。</p>
<p><img src="/blog/img/wps7.jpg" alt="img"> </p>
<p>6）如果在一个终端上运行读取锁程序，在另一个终端上运行写入锁程序，会有什么结果？</p>
<p>写入锁程序会被阻塞，等待读取锁释放锁</p>
<h3 id="2-文件操作仿真FIFO，实现生产者-消费者运行模型"><a href="#2-文件操作仿真FIFO，实现生产者-消费者运行模型" class="headerlink" title="2.文件操作仿真FIFO，实现生产者-消费者运行模型"></a>2.文件操作仿真FIFO，实现生产者-消费者运行模型</h3><p>1）编程实现生产者程序producer.c，创建仿真FIFO结构文件（普通文件），按照给定的时间间隔向FIFO文件写入自动生成的字符（自定义），生产周期及生产的资源数通过参数传递给进程。</p>
<p>Producer.c:</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;fcntl.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;lock_set.c&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> fifo_file <span class="string">&quot;fifo_file.txt&quot;</span></span></span><br><span class="line"><span class="type">void</span> <span class="title function_">produce</span><span class="params">(<span class="type">char</span>* resource)</span>&#123;</span><br><span class="line">    <span class="type">int</span> fd=open(fifo_file,O_CREAT|O_RDWR|O_APPEND,<span class="number">0644</span>);</span><br><span class="line">    <span class="keyword">if</span> (fd &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;打开文件错误\n&quot;</span>);</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    lock_set(fd,F_WRLCK);</span><br><span class="line">    <span class="keyword">if</span> (write(fd, resource, <span class="built_in">strlen</span>(resource))&lt; <span class="number">0</span>)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;写入错误\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;生产：%s\n&quot;</span>,resource);</span><br><span class="line">    lock_set(fd,F_UNLCK);</span><br><span class="line">    close(fd);</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc,<span class="type">char</span> * argv[])</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(argc&lt;<span class="number">3</span>)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;参数错误\n&quot;</span>);</span><br><span class="line">        <span class="built_in">exit</span>(EXIT_FAILURE);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="type">int</span> second=atoi(argv[<span class="number">1</span>]);</span><br><span class="line">    <span class="type">int</span> cnt=atoi(argv[<span class="number">2</span>]);</span><br><span class="line">    <span class="type">int</span> idx=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span>(cnt--)&#123;</span><br><span class="line">        <span class="type">char</span> res[<span class="number">2</span>];</span><br><span class="line">        res[<span class="number">0</span>]=<span class="string">&#x27;A&#x27;</span>+(idx%<span class="number">26</span>);</span><br><span class="line">        res[<span class="number">1</span>]=<span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">        idx++;</span><br><span class="line">        produce(res);</span><br><span class="line">        <span class="comment">//         printf(&quot;等待输入\n&quot;);</span></span><br><span class="line">        <span class="comment">// getchar();</span></span><br><span class="line">        sleep(second);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>2）编程实现消费者程序customer.c，从文件中读取相应数目的字符并在屏幕上显示，然后从文件中删除刚才消费过的数据，可通过两次幅值来实现文件内容的偏移，每次消费的资源通过参数传递给进程。</p>
<p>Customer.c:</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;fcntl.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;lock_set.c&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> fifo_file <span class="string">&quot;fifo_file.txt&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> tmp_file <span class="string">&quot;tmp.txt&quot;</span></span></span><br><span class="line"><span class="type">void</span> <span class="title function_">custom</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">int</span> fd=open(fifo_file,O_RDWR,<span class="number">0644</span>);</span><br><span class="line">    <span class="keyword">if</span>(fd&lt;<span class="number">0</span>)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;文件打开错误\n&quot;</span>);</span><br><span class="line">        <span class="built_in">exit</span>(EXIT_FAILURE);</span><br><span class="line">    &#125;</span><br><span class="line">    lock_set(fd,F_WRLCK);</span><br><span class="line">    <span class="type">char</span> buf[<span class="number">2</span>];</span><br><span class="line">    <span class="type">int</span> size=read(fd,buf,<span class="keyword">sizeof</span>(buf)<span class="number">-1</span>);</span><br><span class="line">    <span class="keyword">if</span>(size&lt;<span class="number">0</span>)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;读取失败\n&quot;</span>);</span><br><span class="line">        close(fd);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span>(size==<span class="number">0</span>)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;资源为空\n&quot;</span>);</span><br><span class="line">        close(fd);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    buf[size]=<span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;消费：%s\n&quot;</span>,buf);</span><br><span class="line">    <span class="type">int</span> fd_tmp=open(tmp_file,O_RDWR|O_CREAT|O_TRUNC,<span class="number">0644</span>);</span><br><span class="line">    <span class="keyword">if</span>(fd_tmp&lt;<span class="number">0</span>)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;文件打开错误\n&quot;</span>);</span><br><span class="line">        <span class="built_in">exit</span>(EXIT_FAILURE);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(read(fd,buf,<span class="keyword">sizeof</span>(buf)<span class="number">-1</span>)&gt;<span class="number">0</span>)&#123;</span><br><span class="line">        write(fd_tmp,buf,<span class="keyword">sizeof</span>(buf)<span class="number">-1</span>);</span><br><span class="line">    &#125;;</span><br><span class="line">    lseek(fd,<span class="number">0</span>,SEEK_SET);</span><br><span class="line">    lseek(fd_tmp,<span class="number">0</span>,SEEK_SET);</span><br><span class="line">    <span class="type">int</span> idx=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span>(read(fd_tmp,buf,<span class="keyword">sizeof</span>(buf)<span class="number">-1</span>))&#123;</span><br><span class="line">        idx++;</span><br><span class="line">        write(fd,buf,<span class="keyword">sizeof</span>(buf)<span class="number">-1</span>);</span><br><span class="line">    &#125;;</span><br><span class="line">    ftruncate(fd,idx);</span><br><span class="line">    close(fd_tmp);</span><br><span class="line">    lock_set(fd,F_UNLCK);</span><br><span class="line">    close(fd);</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc,<span class="type">char</span>* argv[])</span>&#123;</span><br><span class="line">    <span class="type">int</span> num,second;</span><br><span class="line">    <span class="keyword">if</span>(argc&lt;<span class="number">3</span>)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;参数错误&quot;</span>);</span><br><span class="line">        <span class="built_in">exit</span>(EXIT_FAILURE);</span><br><span class="line">    &#125;</span><br><span class="line">    second=atoi(argv[<span class="number">1</span>]);</span><br><span class="line">    num=atoi(argv[<span class="number">2</span>]);</span><br><span class="line">    <span class="keyword">while</span>(num--)&#123;</span><br><span class="line">        custom();</span><br><span class="line">        sleep(second);</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="built_in">exit</span>(EXIT_SUCCESS);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>3）在两个终端上分别运行生产者程序producer和消费者程序customer</p>
<p>运行结果：</p>
<p><img src="/blog/img/wps8.jpg" alt="img"> </p>
<h3 id="3-多路复用—I-O操作及阻塞"><a href="#3-多路复用—I-O操作及阻塞" class="headerlink" title="3. 多路复用—I&#x2F;O操作及阻塞"></a>3. 多路复用—I&#x2F;O操作及阻塞</h3><p>编程实现文件描述符集合的监听 </p>
<p>.multiplex_poll.c</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;fcntl.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/select.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;errno.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAX_BUFFER_SIZE 1024</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> IN_FILES 3</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> TIME_DELAY 60</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAX(a, b) ((a &gt; b) ? (a) : (b))</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span> &#123;</span><br><span class="line">    fd_set rfds;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">timeval</span> <span class="title">tv</span>;</span></span><br><span class="line">    <span class="type">int</span> retval, i, real_read, maxfd;</span><br><span class="line">    <span class="type">int</span> fds[IN_FILES];</span><br><span class="line">    <span class="type">char</span> buf[MAX_BUFFER_SIZE];</span><br><span class="line">    FD_ZERO(&amp;rfds);</span><br><span class="line">    fds[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> ((fds[<span class="number">1</span>] = open(<span class="string">&quot;in1&quot;</span>, O_RDONLY | O_NONBLOCK)) &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Open in1 error\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span> ((fds[<span class="number">2</span>] = open(<span class="string">&quot;in2&quot;</span>, O_RDONLY | O_NONBLOCK)) &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Open in2 error\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    maxfd = MAX(fds[<span class="number">0</span>], MAX(fds[<span class="number">1</span>], fds[<span class="number">2</span>])) + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line">        FD_ZERO(&amp;rfds);</span><br><span class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; IN_FILES; i++) &#123;</span><br><span class="line">            FD_SET(fds[i], &amp;rfds);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        tv.tv_sec = <span class="number">0</span>;</span><br><span class="line">        tv.tv_usec = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        retval = select(maxfd, &amp;rfds, <span class="literal">NULL</span>, <span class="literal">NULL</span>, &amp;tv);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (retval == <span class="number">-1</span>) &#123;</span><br><span class="line">            perror(<span class="string">&quot;Select error&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (retval) &#123;</span><br><span class="line">            <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; IN_FILES; i++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (FD_ISSET(fds[i], &amp;rfds)) &#123;</span><br><span class="line">                    <span class="built_in">memset</span>(buf, <span class="number">0</span>, MAX_BUFFER_SIZE);</span><br><span class="line">                    real_read = read(fds[i], buf, MAX_BUFFER_SIZE);</span><br><span class="line">                    </span><br><span class="line">                    <span class="keyword">if</span> (real_read &lt; <span class="number">0</span>) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (errno != EAGAIN) &#123;</span><br><span class="line">                            perror(<span class="string">&quot;Read error&quot;</span>);</span><br><span class="line">                            <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (real_read == <span class="number">0</span>) &#123;</span><br><span class="line">                        close(fds[i]);</span><br><span class="line">                    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                        <span class="keyword">if</span> (i == <span class="number">0</span>) &#123;</span><br><span class="line">                            <span class="keyword">if</span> ((buf[<span class="number">0</span>] == <span class="string">&#x27;q&#x27;</span>) || (buf[<span class="number">0</span>] == <span class="string">&#x27;Q&#x27;</span>)) &#123;</span><br><span class="line">                                <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                            buf[real_read] = <span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">                            <span class="built_in">printf</span>(<span class="string">&quot;%s&quot;</span>, buf);</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<p>运行时，需要打开3个虚拟终端，分别创建两个管道文件in1和in2，运行主程序</p>
<p>终端1：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">mknod in1 p</span><br><span class="line"></span><br><span class="line">cat &gt;in1</span><br><span class="line"></span><br><span class="line">MULTIPLEX CALL</span><br><span class="line"></span><br><span class="line">TEST IN1</span><br><span class="line"></span><br><span class="line">END</span><br></pre></td></tr></table></figure>



<p>终端2：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">mknod in2 p</span><br><span class="line"></span><br><span class="line">multiplex call</span><br><span class="line"></span><br><span class="line">test in2</span><br><span class="line"></span><br><span class="line">end</span><br></pre></td></tr></table></figure>



<p>终端3:</p>
<p>.&#x2F;multiplex_poll.c</p>
<h2 id="五、实验小结"><a href="#五、实验小结" class="headerlink" title="五、实验小结"></a>五、<strong>实验小结</strong></h2><p><strong>1遇到的问题，消费者生产者模型出现消息丢失</strong></p>
<p><strong>原本我不是采用复制原文件到临时文件再从临时文件复制到源文件，而是直接采用rename函数改名，将临时文件改为源文件的名字，但是这种情况出现了消息丢失，我找了好久的bug，终于发现就是这个rename的原因，在高并发或者手动调试断点的情况下就会阻塞等待加锁的进程可以加锁时，加锁的文件可能已经不被任何inode引用，尽管它还是成功写入数据了，但是另一个消费者可能就消费不到这个生产者刚刚生产的这个消息。</strong></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/lanceluot/blog.git/2024/05/01/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%AE%9E%E9%AA%8C3-%E6%96%87%E4%BB%B6io%E7%BC%96%E7%A8%8B/" data-id="clvnfbuu80000g05bdta8alux" data-title="嵌入式实验3-文件io编程" class="article-share-link"><span class="fa fa-share">Teilen</span></a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/%E5%AE%9E%E9%AA%8C/" rel="tag">实验</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/blog/2024/05/01/%E7%AE%97%E6%B3%95%E8%AE%BE%E8%AE%A1%E4%B8%8E%E5%88%86%E6%9E%90%E5%AE%9E%E9%AA%8C%E4%B8%89-%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Neuer</strong>
      <div class="article-nav-title">
        
          算法设计与分析实验三-动态规划
        
      </div>
    </a>
  
  
    <a href="/blog/2024/04/30/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E8%B7%B5-%E5%AE%9E%E7%8E%B0%E4%B8%80%E4%B8%AAfat12%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Älter</strong>
      <div class="article-nav-title">操作系统实践-实现一个fat12文件系统</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Kategorien</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA/">计算机</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/BUG/" rel="tag">BUG</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/RPC/" rel="tag">RPC</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/git/" rel="tag">git</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/java/" rel="tag">java</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/java%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/" rel="tag">java后端开发</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/java%E6%BA%90%E7%A0%81/" rel="tag">java源码</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/jvm/" rel="tag">jvm</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/leetcode/" rel="tag">leetcode</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/leetcode%E9%A2%98%E8%A7%A3/" rel="tag">leetcode题解</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/rabbitmq/" rel="tag">rabbitmq</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/redis/" rel="tag">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/shell/" rel="tag">shell</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/spring/" rel="tag">spring</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/springboot/" rel="tag">springboot</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/test/" rel="tag">test</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E4%BD%9C%E4%B8%9A/" rel="tag">作业</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E5%AE%9E%E9%AA%8C/" rel="tag">实验</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E5%AE%9E%E9%AA%8C3/" rel="tag">实验3</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E6%84%9F%E6%83%B3/" rel="tag">感想</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="tag">操作系统</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" rel="tag">数据结构与算法</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E6%B5%8B%E8%AF%95/" rel="tag">测试</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E7%96%91%E9%97%AE/" rel="tag">疑问</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E7%AC%94%E8%AE%B0/" rel="tag">笔记</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E7%AE%97%E6%B3%95/" rel="tag">算法</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" rel="tag">设计模式</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E9%97%AE%E9%A2%98/" rel="tag">问题</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/blog/tags/BUG/" style="font-size: 10px;">BUG</a> <a href="/blog/tags/RPC/" style="font-size: 10px;">RPC</a> <a href="/blog/tags/git/" style="font-size: 10px;">git</a> <a href="/blog/tags/java/" style="font-size: 20px;">java</a> <a href="/blog/tags/java%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/" style="font-size: 10px;">java后端开发</a> <a href="/blog/tags/java%E6%BA%90%E7%A0%81/" style="font-size: 10px;">java源码</a> <a href="/blog/tags/jvm/" style="font-size: 10px;">jvm</a> <a href="/blog/tags/leetcode/" style="font-size: 10px;">leetcode</a> <a href="/blog/tags/leetcode%E9%A2%98%E8%A7%A3/" style="font-size: 15px;">leetcode题解</a> <a href="/blog/tags/rabbitmq/" style="font-size: 10px;">rabbitmq</a> <a href="/blog/tags/redis/" style="font-size: 10px;">redis</a> <a href="/blog/tags/shell/" style="font-size: 10px;">shell</a> <a href="/blog/tags/spring/" style="font-size: 10px;">spring</a> <a href="/blog/tags/springboot/" style="font-size: 10px;">springboot</a> <a href="/blog/tags/test/" style="font-size: 10px;">test</a> <a href="/blog/tags/%E4%BD%9C%E4%B8%9A/" style="font-size: 10px;">作业</a> <a href="/blog/tags/%E5%AE%9E%E9%AA%8C/" style="font-size: 15px;">实验</a> <a href="/blog/tags/%E5%AE%9E%E9%AA%8C3/" style="font-size: 10px;">实验3</a> <a href="/blog/tags/%E6%84%9F%E6%83%B3/" style="font-size: 10px;">感想</a> <a href="/blog/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" style="font-size: 10px;">操作系统</a> <a href="/blog/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" style="font-size: 10px;">数据结构与算法</a> <a href="/blog/tags/%E6%B5%8B%E8%AF%95/" style="font-size: 10px;">测试</a> <a href="/blog/tags/%E7%96%91%E9%97%AE/" style="font-size: 10px;">疑问</a> <a href="/blog/tags/%E7%AC%94%E8%AE%B0/" style="font-size: 10px;">笔记</a> <a href="/blog/tags/%E7%AE%97%E6%B3%95/" style="font-size: 10px;">算法</a> <a href="/blog/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" style="font-size: 15px;">设计模式</a> <a href="/blog/tags/%E9%97%AE%E9%A2%98/" style="font-size: 10px;">问题</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archiv</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2024/05/">May 2024</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2024/04/">April 2024</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2024/03/">March 2024</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2024/02/">February 2024</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2024/01/">January 2024</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2023/10/">October 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2023/09/">September 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2023/08/">August 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2023/05/">May 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2023/04/">April 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2022/12/">December 2022</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">letzter Beitrag</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/blog/2024/05/01/%E7%AE%97%E6%B3%95%E8%AE%BE%E8%AE%A1%E4%B8%8E%E5%88%86%E6%9E%90%E5%AE%9E%E9%AA%8C%E4%B8%89-%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/">算法设计与分析实验三-动态规划</a>
          </li>
        
          <li>
            <a href="/blog/2024/05/01/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%AE%9E%E9%AA%8C3-%E6%96%87%E4%BB%B6io%E7%BC%96%E7%A8%8B/">嵌入式实验3-文件io编程</a>
          </li>
        
          <li>
            <a href="/blog/2024/04/30/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E8%B7%B5-%E5%AE%9E%E7%8E%B0%E4%B8%80%E4%B8%AAfat12%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F/">操作系统实践-实现一个fat12文件系统</a>
          </li>
        
          <li>
            <a href="/blog/2024/04/25/LambdaQuery-%E5%92%8C-Query%E7%9A%84%E5%8C%BA%E5%88%AB/">LambdaQuery 和 Query的区别</a>
          </li>
        
          <li>
            <a href="/blog/2024/04/25/LambdaQuery%E5%92%8CQuery%E7%9A%84%E5%8C%BA%E5%88%AB/">LambdaQuery和Query的区别</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2024 myself<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/blog/" class="mobile-nav-link">Home</a>
  
    <a href="/blog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/blog/js/jquery-3.6.4.min.js"></script>



  
<script src="/blog/fancybox/jquery.fancybox.min.js"></script>




<script src="/blog/js/script.js"></script>





  </div>
</body>
</html>